//
//  ZYStudentDetailController.m
//  BestLearning
//
//  Created by Ethan on 2017/10/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYStudentDetailController.h"
#import "ZYStudentDetailCell.h"
#import "ZYstudentDetailModel.h"
#import "ZYStudentSearchController.h"
#import "LLNavigationMidSearchItem.h"
#import "HMStudentRenewdetails.h"
#define kCell @"cell"
@interface ZYStudentDetailController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@property (nonatomic,strong) LLNavigationMidSearchItem *midItem;
@property(nonatomic, strong) UITextField *searchTextField;
@end

@implementation ZYStudentDetailController



- (NSMutableArray *)dataSource {

    if (_dataSource == nil) {
        self.dataSource  = [NSMutableArray array];
    }

    return _dataSource;


}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpSuperView];
    [self setUpRefresh];
    [self initNavigationBar];
    
}


-(void)initNavigationBar{
    
    
    //NavigationItem设置属性
    self.midItem = [[LLNavigationMidSearchItem alloc]init];
    self.midItem.frame = CGRectMake(0, 0,260, 30);
    
       self.navigationItem.titleView =self. midItem;
    
    
    self.searchTextField = self.midItem.searchField;
    self.searchTextField.delegate = self;
    self.searchTextField.returnKeyType =  UIReturnKeySearch;
    // 添加账号
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"续费详情" style:(UIBarButtonItemStylePlain) target:self action:@selector(seeRenewDetails)];
}
































#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}







- (void)setNetWork{
    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=bindStudentsDetial&pageNumber=1
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=bindStudentsDetial&pageNumber=%ld",(long)self.page];
   

    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
  
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];   
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
            
            NSArray *arr = [dict valueForKey:@"dataList"];
            
            
            for (NSDictionary *dic in arr) {
                
                ZYstudentDetailModel *model = [ZYstudentDetailModel new];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataSource addObject:model];
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
}













#pragma mark -----子视图
- (void)setUpSuperView{
    
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight -64)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 200;
    
    
    [self.tableView registerClass:[ZYStudentDetailCell  class] forCellReuseIdentifier:kCell];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYStudentDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    
    
    if (self.dataSource.count > indexPath.row) {
        
        
        ZYstudentDetailModel *model = self.dataSource[indexPath.row];
        
        
    
        [cell assignByZYstudentDetailModel:model];
        
        
        [cell.phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    return cell;
}



- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    

    [self.searchTextField resignFirstResponder];
    
    [SVProgressHUD dismiss];
    
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    [self.searchTextField resignFirstResponder];
}

//收回键盘
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.searchTextField resignFirstResponder];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (self.searchTextField.text.length!=0) {
        
        ZYStudentSearchController *vc = [[ZYStudentSearchController alloc]init];
        vc.title = @"搜索结果";
        vc.textStr = self.searchTextField.text;
        
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [MBProgressHUD showError:@"请输入搜索内容"];
    }
    
    return YES;
    
}


- (void)phoneButtonAction:(UIButton *)sender{
    
    ZYStudentDetailCell *cell = (ZYStudentDetailCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYstudentDetailModel *model = self.dataSource[indexPath.row];
    
    
    
    
    
    [self phoneChat:[NSString stringWithFormat:@"%@",[model.owner valueForKey:@"phone"]]];
    
    
    
    
}

- (void)phoneChat:(NSString *)phoneNumber{
    
    NSString *strs = phoneNumber;
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",strs];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}


#pragma mark - 续费详情

- (void)seeRenewDetails {
    HMStudentRenewdetails *vc = [HMStudentRenewdetails new];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
